package com.system.connect.springboot_flowers.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.connect.springboot_flowers.dto.FlowersDto;
import com.system.connect.springboot_flowers.entity.Flowers;
import com.system.connect.springboot_flowers.mapper.FlowersMapper;
import com.system.connect.springboot_flowers.service.IFlowersService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * <p>
 * 花种表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2025-01-13
 */
@Service
public class FlowersServiceImpl extends ServiceImpl<FlowersMapper, Flowers> implements IFlowersService {

    private static final Logger logger = (Logger) LoggerFactory.getLogger(IFlowersService.class);

    /**
     * 创建一个新的花朵记录。
     *
     * @param flowersDto 包含花朵信息的数据传输对象。
     * @return 如果创建成功则返回 true，否则返回 false。
     */
    @Transactional
    public boolean create(FlowersDto flowersDto) {
        try {
            Flowers flowers = new Flowers();
            flowers.setName(flowersDto.getName());

            // 处理季节字段，将其转换为逗号分隔的字符串
            String seasonString = flowersDto.getSeason() != null ? String.join(",", flowersDto.getSeason()) : null;
            flowers.setSeason(seasonString);
            //处理特殊属性字段，将其转换为逗号分隔的字符串
            String specialAttributeString = flowersDto.getSpecialAttribute() != null ? String.join(",", flowersDto.getSpecialAttribute()) : null;
            flowers.setSpecialAttribute(specialAttributeString);
            //处理标签字段，将其转换为逗号分隔的字符串
            String tagsString = flowersDto.getTags() != null ? String.join(",", flowersDto.getTags()) : null;
            flowers.setTags(tagsString);

            // 处理开花周期，确保日期格式正确
            if (flowersDto.getDate1() != null && flowersDto.getDate2() != null) {
                String bloomPeriod = flowersDto.getDate1() + " - " + flowersDto.getDate2();
                flowers.setBloomPeriod(bloomPeriod);
            }

            flowers.setColor(flowersDto.getColor());
            flowers.setMenaning(flowersDto.getMenaning());
            flowers.setPopularity(flowersDto.getPopularity());
            flowers.setPrice(flowersDto.getPrice());
            flowers.setCategory(flowersDto.getCategory());
            flowers.setStatus(flowersDto.getStatus());
            flowers.setStock(flowersDto.getStock());
            flowers.setIntroduction(flowersDto.getIntroduction());
            flowers.setFlowerPhoto(flowersDto.getFlowerPhoto());
            flowers.setDifficulty(flowersDto.getDifficulty());
            save(flowers);

            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
